package com.zzx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.domain.LoginUser;
import com.zzx.domain.Role;
import com.zzx.domain.Vo.AddRoleVo;
import com.zzx.domain.Vo.PermissionVO2;
import com.zzx.domain.Vo.PermissionVo;
import com.zzx.domain.Vo.RolesList;
import com.zzx.exception.ExceptionState;
import com.zzx.exception.SystemException;
import com.zzx.mapper.RoleMapper;
import com.zzx.service.PermissionService;
import com.zzx.service.RoleService;
import com.zzx.util.ResponseResult;
import com.zzx.util.SecurityContextHolderUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2022-08-01 17:54:34
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private PermissionServiceImpl permissionService;

    /**
     * 获取当前用户的角色
     * @return
     */
    @Override
    public Role getRole() {
        LoginUser loginUser = SecurityContextHolderUtils.getLoginUser();
        Integer id = loginUser.getManager().getRoleId();
        if(id==0){
            return new Role(0,null,null,null,null);
        }
        Role byId = getById(id);

        return byId;
    }

    /**
     * 获取角色列表
     * @return
     */
    @Override
    public ResponseResult RoleList() {
        //获取所有的角色
        List<Role> list = list();
        //第一层角色信息
        List<RolesList> collect = list.stream().map(item ->
                new RolesList(item.getRoleId(), item.getRoleName(), item.getRoleDesc(), permissionService.permissionVoList(item))
        ).collect(Collectors.toList());
        // 第二到第四层都是权限说明

        return new ResponseResult("获取成功", ExceptionState.OK,collect);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @Override
    public ResponseResult addRole(Role role) {
        boolean save = save(role);
        if(save==false) throw new SystemException(ExceptionState.UNCRESTEROLE);
        AddRoleVo addRoleVo = new AddRoleVo();
        BeanUtils.copyProperties(role,addRoleVo);
        return new ResponseResult("创建成功",ExceptionState.CREATED,addRoleVo);
    }

    /**
     * 根据角色id查询信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult selectRoleById(Integer id) {
        Role byId = getById(id);
        AddRoleVo addRoleVo = new AddRoleVo();
        BeanUtils.copyProperties(byId,addRoleVo);
        return new ResponseResult("获取成功",ExceptionState.OK,addRoleVo);
    }

    /**
     * 根据更新角色信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult updateRoleById(Integer id,AddRoleVo addRoleVo) {
        Role role = new Role(id, addRoleVo.getRoleName(), null, null, addRoleVo.getRoleDesc());
        boolean b = updateById(role);
        if(b==false) throw new SystemException(ExceptionState.NOUPDATE);
        AddRoleVo addRoleVo1 = new AddRoleVo();
        BeanUtils.copyProperties(role,addRoleVo);
        return new ResponseResult("获取成功",ExceptionState.OK,addRoleVo1);
    }

    /**
     * 根据id删除角色
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteRoleById(Integer id) {
        boolean b = removeById(id);
        if(b==false) throw new SystemException(ExceptionState.NODELETE);
        return new ResponseResult("删除成功",ExceptionState.OK);
    }

    /**
     * 角色授权
     * @param roleId
     * @param rids
     * @return
     */
    @Override
    public ResponseResult setPermission(Integer roleId, String rids) {
        Role role = new Role();
        role.setRoleId(roleId);
        role.setPsIds(rids);
        boolean b = updateById(role);
        if(b==false) throw new SystemException(ExceptionState.NOUPDATE);
        return new ResponseResult("更新成功",ExceptionState.OK);
    }

    /**
     * 删除角色的指定id
     * @param roleId
     * @param rightId
     * @return
     */
    @Override
    public ResponseResult deleteByPermission(Integer roleId, Integer rightId) {
        /*查询出角色的信息*/
        Role byId = getById(roleId);
        /*分割出所有权限*/
        String[] split = byId.getPsIds().split(",");
        StringBuffer buffer = new StringBuffer();
        for (int i = 0;i<split.length;i++){
            if(split[i].equals(rightId.toString()))continue;
            buffer.append(split[i]+",");
        }
        /*清除完毕后 再更新到数据库中*/
        Role role = new Role();
        role.setRoleId(roleId);
        role.setPsIds(buffer.toString());
        boolean b = updateById(role);
        if(b==false) throw new SystemException(ExceptionState.NOUPDATE);
        /*更新完毕之后查询*/
        List<PermissionVo> permissionVos = permissionService.permissionVoList(role);
        return new ResponseResult("取消权限成功",ExceptionState.OK,permissionVos);
    }


}


